//
//  CityListPopupViewController.m
//  CreditHub
//
//  Created by chenglang on 2024/9/22.
//

#import "CityListPopupViewController.h"
#import "CityListTableViewCell.h"
#import "HomeRequest.h"

@interface CityListPopupViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *buttonSelect1;
@property (weak, nonatomic) IBOutlet UILabel *labelLine;

@property (weak, nonatomic) IBOutlet UIButton *buttonSelect2;
@property (weak, nonatomic) IBOutlet UILabel *labelLine2;

@property (nonatomic, strong) NSArray *arrayList;
@property (nonatomic, assign) NSInteger level;
@property (nonatomic, assign) NSInteger select;
@property (nonatomic, assign) NSInteger select1;

@end

@implementation CityListPopupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.level = 1;
    self.select = -1;
    self.select1 = -1;
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.arrayList = [NSArray arrayWithArray:[defaults objectForKey:@"CityListKey"]];
    if(self.arrayList.count == 0){
        [self requestCityList];
    }
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CityListTableViewCell" bundle:nil] forCellReuseIdentifier:@"CityListTableViewCell"];
    
}

- (void)requestCityList{
    
    __weak typeof(self) weakSelf = self;
    [HomeRequest requestCityListSuccess:^(NSURLSessionDataTask *task, id response) {
        if([response[@"ofexcitement"] integerValue] == 0){
            
            weakSelf.arrayList = response[@"pity"];
            [weakSelf.tableView reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
- (IBAction)clickSelect1:(id)sender {
    
    self.buttonSelect2.hidden = YES;
    self.labelLine2.hidden = YES;
    self.select1 = -1;
    [self.tableView reloadData];
    self.level = 1;
    self.labelLine.hidden = NO;
    [self.buttonSelect1 setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    
}
- (IBAction)clickSelect2:(id)sender {
    
}
- (IBAction)clickClose:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if(self.level == 2){
        NSArray *arr = [NSArray arrayWithArray:self.arrayList[self.select][@"tears"]];
        return arr.count;
    }
    
    return self.arrayList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CityListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CityListTableViewCell"];
    
    cell.labelCity.textColor = colorWithHex(0xA9A6A6);
    cell.labelCity.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    cell.contentView.backgroundColor = UIColor.whiteColor;
    if(self.level == 1){
        NSDictionary *dic = self.arrayList[indexPath.row];
        cell.labelCity.text = dic[@"wouldwonder"];
        if(self.select == indexPath.row){
            cell.labelCity.textColor = UIColor.blackColor;
            cell.labelCity.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
            cell.contentView.backgroundColor = AppThemeColor;
        }
    }else{
        NSArray *arr = [NSArray arrayWithArray:self.arrayList[self.select][@"tears"]];
        cell.labelCity.text = arr[indexPath.row][@"wouldwonder"];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(self.level==1){
        self.level = 2;
        self.buttonSelect2.hidden = NO;
        self.labelLine2.hidden = NO;
        self.labelLine.hidden = YES;
        [self.buttonSelect1 setTitleColor:colorWithHex(0x525252) forState:UIControlStateNormal];
        [self.buttonSelect1 setTitle:self.arrayList[indexPath.row][@"wouldwonder"] forState:UIControlStateNormal];
        self.select = indexPath.row;
        [self.tableView reloadData];
    }else{
        __weak typeof(self) weakSelf = self;
        [self dismissViewControllerAnimated:YES completion:^{
            if(weakSelf.selectBlock){
                NSString *city = weakSelf.arrayList[weakSelf.select][@"wouldwonder"];
                
                NSArray *arr = [NSArray arrayWithArray:weakSelf.arrayList[weakSelf.select][@"tears"]];
                city = [NSString stringWithFormat:@"%@|%@",city, arr[indexPath.row][@"wouldwonder"]];
                weakSelf.selectBlock(city);
            }
        }];
        
    }
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
